package com.elong.businesstravel.modules.home.dialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.elong.businesstravel.ELongBusinessTravelApplication;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.entitys.Chains;
import com.elong.businesstravel.modules.home.adapter.SimpleGridDialogAdapter;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 酒店品牌
 * 
 * @author lenovo
 * 
 */
public class HotelBrandDialog extends Dialog {

	private Context mContext;
	private ELongBusinessTravelApplication mApplication;
	private GridView mGridView;
	private SimpleGridDialogAdapter mSimpleGridAdapter;
	private OnItemClickListener mOnItemClickListener;
	private List<Chains> mChains;
	private String mSelectChainId;

	public HotelBrandDialog(Context context) {
		super(context);
		init(context);
	}

	public HotelBrandDialog(Context context, int theme, List<Chains> chains, String selectChainId) {
		super(context, theme);
		mChains = chains;
		mSelectChainId = selectChainId;
		init(context);
	}

	public HotelBrandDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		init(context);
	}

	private void init(Context context) {
		Window window = getWindow();
		window.setGravity(Gravity.BOTTOM);

		mContext = context;
		mApplication = (ELongBusinessTravelApplication) mContext
				.getApplicationContext();

		setContentView(R.layout.dialog_hotel_brand);

		List<String> chainStrings = new ArrayList<String>();
		String selectName = "不限";
		for (int i = 0; i < mChains.size(); i++) {
			chainStrings.add(mChains.get(i).shortname);
			if (mChains.get(i).brandid
					.equals(mSelectChainId)) {
				selectName  = mChains.get(i).shortname;
			}
		}

		mGridView = (GridView) findViewById(R.id.gridView);
		mSimpleGridAdapter = new SimpleGridDialogAdapter(mContext, chainStrings, selectName);
		mGridView.setAdapter(mSimpleGridAdapter);

		setTitleNavBar();

		setListener();
		
		setCanceledOnTouchOutside(true);
	}

	protected void setListener() {

		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				if (null != mOnItemClickListener) {
					String chainNameString = (String) mSimpleGridAdapter
							.getItem(position);

					view.setTag(chainNameString);
					mOnItemClickListener
							.onItemClick(parent, view, position, id);

				}
			}
		});
	}

	public void setOnItemListener(OnItemClickListener onItemClickListener) {
		mOnItemClickListener = onItemClickListener;
	}

	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("选择品牌");
		titleNavBarView.setTitleBackground(R.color.bg_title_dialog);
		titleNavBarView.setLeftButtonImageString(0, "取消", R.color.text_blue);
		titleNavBarView
				.setLeftButtonOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						cancel();
					}
				});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);

	}

}
